package com.boot.security.server.controller.system;

import java.util.List;

import com.boot.security.server.dao.mffl.MfflUserDao;
import com.boot.security.server.model.mffl.MfflUser;
import com.boot.security.server.service.impl.mffl.MfflSystemMessages.IMfflSystemMessages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.dao.mffl.MfflSystemMessagesDao;
import com.boot.security.server.model.mffl.MfflSystemMessages;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/mfflSystemMessagess")
public class MfflSystemMessagesController {

    @Autowired
    private MfflSystemMessagesDao mfflSystemMessagesDao;
    @Autowired
    private MfflUserDao mfflUserDao;
    @Autowired
    private IMfflSystemMessages iMfflSystemMessages;

    @PostMapping
    @ApiOperation(value = "保存")
    @PreAuthorize("hasAnyAuthority('sys:systemMessages:add')")
    public MfflSystemMessages save(@RequestBody MfflSystemMessages mfflSystemMessages) {

        if (mfflSystemMessages.getSelect().equals("1")){
            iMfflSystemMessages.sendMessage("您收到一条系统消息，内容为:"+mfflSystemMessages.getContent(),mfflSystemMessages.getType(),null,mfflSystemMessages.getUserId());
        }else {
            List<MfflUser> list = this.mfflUserDao.selectMfflUserAllList();
            for (int i = 0; i < list.size(); i++) {
                mfflSystemMessages.setUserId(list.get(i).getUserid());
                iMfflSystemMessages.sendMessage("您收到一条系统消息，内容为:"+mfflSystemMessages.getContent(),mfflSystemMessages.getType(),null,mfflSystemMessages.getUserId());
            }
        }

        return mfflSystemMessages;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    @PreAuthorize("hasAnyAuthority('sys:systemMessages:query')")
    public MfflSystemMessages get(@PathVariable Long id) {
        return mfflSystemMessagesDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @PreAuthorize("hasAnyAuthority('sys:systemMessages:add')")
    public MfflSystemMessages update(@RequestBody MfflSystemMessages mfflSystemMessages) {
        mfflSystemMessagesDao.update(mfflSystemMessages);

        return mfflSystemMessages;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    @PreAuthorize("hasAnyAuthority('sys:systemMessages:query')")
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflSystemMessagesDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MfflSystemMessages> list(PageTableRequest request) {
                return mfflSystemMessagesDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @PreAuthorize("hasAnyAuthority('sys:systemMessages:del')")
    public void delete(@PathVariable Long id) {
        mfflSystemMessagesDao.delete(id);
    }
}
